package com.shine.app.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.shine.app.AppException;
import com.shine.app.AppManager;
import com.shine.app.R;
import com.shine.app.adapter.LocationAdapter;
import com.shine.app.bean.PlaceNearBean;
import com.shine.app.bean.request.LocationNearBean;
import com.shine.app.bean.responses.PlaceNearResponse;
import com.shine.app.commons.Commons;

public class LocationResultActivity extends LocationBaseActivity implements
		OnClickListener {

	private NearMeAsyncTask nearMeAsyncTask;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.viewpager_location);
		InitViewPagerView();
		InitPagerImageView();
		InitViewPager();
		init();
		initRecentsCount();

		getData(Commons.LISTVIEW_TAB_ZERO);
		getData(Commons.LISTVIEW_TAB_ONE);
		getData(Commons.LISTVIEW_TAB_TWO);
		clickListener();
	}

	protected void clickListener() {
		super.clickListener();
		textView1.setOnClickListener(this);
		textView2.setOnClickListener(this);
		textView3.setOnClickListener(this);
		viewPager.setOnPageChangeListener(new PagerOnPageChangeListener());
		searchImageView.setOnClickListener(this);
		gobackImageView.setOnClickListener(this);
		headNameTextView.setOnClickListener(this);
	}

	protected void getData(int index) {
		if (index == Commons.LISTVIEW_TAB_ZERO) {
			getMyLocation();
			LocationNearBean locationNearBean = getRequestdata();
			nearMeAsyncTask = new NearMeAsyncTask();
			nearMeAsyncTask.execute(locationNearBean);
		} else if (index == Commons.LISTVIEW_TAB_ONE) {
			progressBar_recent.setVisibility(View.GONE);
			List<PlaceNearBean> list = dataBaseManager.find();
			LocationAdapter adapter = new LocationAdapter(
					LocationResultActivity.this, list);
			recentListView.setAdapter(adapter);
		} else if (index == Commons.LISTVIEW_TAB_TWO) {
			List<PlaceNearBean> popularResult = PlaceNearResponse
					.getPopularResult();
			LocationAdapter adapter = new LocationAdapter(this, popularResult);
			popularListView.setAdapter(adapter);
		} else
			;
	}

	public class NearMeAsyncTask extends
			AsyncTask<Object, Integer, List<PlaceNearBean>> {
		private LocationNearBean locationNearBean;

		@Override
		protected List<PlaceNearBean> doInBackground(Object... params) {
			List<PlaceNearBean> list = null;
			try {
				locationNearBean = (LocationNearBean) params[0];
				list = appContext.locationByNear(locationNearBean);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if (list == null) {
				list = new ArrayList<PlaceNearBean>();
			}
			return list;
		}

		@Override
		protected void onPostExecute(List<PlaceNearBean> result) {
			super.onPostExecute(result);
			LocationAdapter adapter = null;
			adapter = new LocationAdapter(LocationResultActivity.this, result);
			nearListView.setAdapter(adapter);
			progressBar_near.setVisibility(View.GONE);
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar_near.setVisibility(View.VISIBLE);
		}
	}

	private LocationNearBean getRequestdata() {
		LocationNearBean locationNearBean = new LocationNearBean();
		locationNearBean.setName("");
		locationNearBean.setType(Commons.LISTVIEW_TAB_ZERO);
		locationNearBean.setCount(Commons.PAGE_SIZE);
		locationNearBean.setLatitude(appContext.getLatitude());
		locationNearBean.setLongitude(appContext.getLongitude());
		return locationNearBean;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.text1:
			if (Commons.LISTVIEW_TAB_ZERO == currIndex) {
				return;
			}
			currIndex = Commons.LISTVIEW_TAB_ZERO;
			viewPager.setCurrentItem(currIndex);
			break;

		case R.id.text2:
			if (Commons.LISTVIEW_TAB_ONE == currIndex) {
				return;
			}
			currIndex = Commons.LISTVIEW_TAB_ONE;
			viewPager.setCurrentItem(currIndex);
			break;

		case R.id.text3:
			if (Commons.LISTVIEW_TAB_TWO == currIndex) {
				return;
			}
			currIndex = Commons.LISTVIEW_TAB_TWO;
			viewPager.setCurrentItem(currIndex);
			break;

		case R.id.head_seacher:
			Intent intent = new Intent(this, LocationSearchActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			intent.putExtra("searchBean", searchBean);
			this.startActivity(intent);
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.slide_in_up, R.anim.anim);
			break;

		case R.id.head_back:
		case R.id.head_name:
			AppManager.getAppManager().finishActivity();
			overridePendingTransition(R.anim.anim, R.anim.slide_out_down);
			break;

		default:
			break;
		}
	}

	@Override
	protected void onDestroy() {
		if (nearMeAsyncTask != null
				&& nearMeAsyncTask.getStatus() == AsyncTask.Status.RUNNING) {
			nearMeAsyncTask.cancel(true);
		}
		super.onDestroy();
	}

}
